#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "../core/preference.h"
#include "../core/dtlibrary.h"
#include "../core/dttrack.h"
#include "../database/connection.h"
#include "library/table/walbum.h"

#include "wprogressbar.h"
#include <QFileDialog>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    /* Create the database if not exists */
    Connection *con = Connection::getInstance();
    if(con == NULL)
        exit(1);
    collection = new Collection();

    /* Create the preference file (or open it) */
    Preference *pref = Preference::getInstance();
    QSet<DtLibrary> sLib = pref->getLibrary();
    if(sLib.isEmpty())
    {
        /* Disblay the welcome window */
        welcome = new Welcome();
        ui->verticalLayout->addWidget(welcome);
        QObject::connect(welcome, SIGNAL(librairyAdded()), this, SLOT(start()));
    }
    else
    {
        /* Display the collection with player, playlist and all stuff. */
        welcome = NULL;
        start();
    }
}

void MainWindow::start()
{
    if(welcome)
    {
        ui->verticalLayout->removeWidget(welcome);
        welcome->deleteLater();
    }
    WProgressBar *progress = WProgressBar::getInstance(this);
    progress->stop();
    ui->statusBar->addWidget(progress);
    central = new WLibrary(collection);
    ui->verticalLayout->addWidget(central);
}

MainWindow::~MainWindow()
{
//    collection.deleteLater();
//    collection.~Collection();
    delete ui;
}
